package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class Rebate extends BaseRepair {

    public Rebate(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }
    //触发礼包一条龙三个日志
    public void go1(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getData();
        listMap = getBuySecret(listMap);
        listMap = getBuyFund(listMap);
        listMap = getBuyCard(listMap);
        DataMgr.addData(listMap);
//        addRebateData(listMap);
    }

    private List<Map<String, String>> getData() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT uu.userId,uu.openId,uu.platformUserId,uu.vipExp FROM t_u_user uu WHERE uu.vipExp > 0";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("openId",checkNullToEmpty(rs.getString("openId")));
                temp.put("platformUserId",checkNullToEmpty(rs.getString("platformUserId")));
                temp.put("vipExp",checkNullToEmpty(rs.getString("vipExp")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getBuySecret(List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_secret_volume WHERE userId = ? AND isBuy = 1";
        try {
            for(Map<String,String> data : idataList){
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1,data.get("userId"));
                rs = call.executeQuery();
                if(rs.next()){
                    data.put("buySecret","1");
                }else{
                    data.put("buySecret","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getBuyFund(List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            for(Map<String,String> data : idataList){
                long userId = Long.parseLong(data.get("userId"));
                long attr_index = userId%16;
                String sql = "SELECT * FROM t_u_attr_"+attr_index+" WHERE userId = ? AND attrId = 11 AND `value` = 1";
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1,data.get("userId"));
                rs = call.executeQuery();
                if(rs.next()){
                    data.put("buyFund","1");
                }else{
                    data.put("buyFund","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getBuyCard(List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_card WHERE userId = ?";
        try {
            for(Map<String,String> data : idataList){
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1,data.get("userId"));
                rs = call.executeQuery();
                data.put("buyYear","0");
                data.put("buyMonth","0");
                if(rs.next()){
                    if(rs.getLong("lifeCardEndTime") > 0){
                        data.put("buyYear","1");
                    }
                    if(rs.getLong("weekCardEndTime") > 0){
                        data.put("buyMonth","1");
                    }
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }


    public boolean addRebateData(List<Map<String,String>> list) {
        boolean result = false;
        String sql = "INSERT INTO t_u_rebate" +
                " (userId,openId,platformUserId,vipExp,buySecret,buyFund,buyYear,buyMonth,isGetVipExp,isGetSecret,isGetFund,isGetYear,isGetMonth) " +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (Map<String,String> data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("userId")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("openId")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("platformUserId")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("vipExp")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("buySecret")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("buyFund")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("buyYear")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.get("buyMonth")));
            para.put(i++, new DbParameter(Types.INTEGER,0));
            para.put(i++, new DbParameter(Types.INTEGER,0));
            para.put(i++, new DbParameter(Types.INTEGER,0));
            para.put(i++, new DbParameter(Types.INTEGER,0));
            para.put(i++, new DbParameter(Types.INTEGER,0));
            mapList.add(para);
        }

        if (getGameServer().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }
}
